package com.starinside.wp.controller.api;

import com.starinside.wp.config.WxMpProperties;
import com.starinside.wp.domain.CheckRecord;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.vo.ChildrenCheckRecordVO;
import com.starinside.wp.repository.CheckRecordNativeRepository;
import com.starinside.wp.repository.CheckRecordRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@EnableConfigurationProperties(WxMpProperties.class)
@RestController
@RequestMapping("/wx/api/checkin")
@Api(value = "/wx/api/checkin", tags = "签到签退")
public class APICheckinController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    CheckRecordRepository checkRecordRepository;

    @Autowired
    CheckRecordNativeRepository checkRecordNativeRepository;

    @RequestMapping(value = "/monthRecords", method = RequestMethod.GET)
    @ApiOperation(value = "月签到列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "查询的月份，如 2019/06"),
            @ApiImplicitParam(name = "childrenId", value = "宝宝id，若没有则使用默认宝宝id")
    })
    public StatusResponse getMonthRecords(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp, String month, String childrenId){

        if(StringUtils.isEmpty(childrenId)) {
            WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

            if (loginUser.getDefaultBabyId() == null) {
                return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "当前家长没有就读的宝宝");
            }

            childrenId = loginUser.getDefaultBabyId();
        }
        List<ChildrenCheckRecordVO> items = checkRecordNativeRepository.findMonthRecordByChildrenId(childrenId, month);

        return StatusResponse.success(items);
    }


    @RequestMapping(value = "/dailyRecords", method = RequestMethod.GET)
    @ApiOperation(value = "当天签到详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "day", value = "查询的日期，如 2019/06/01"),
            @ApiImplicitParam(name = "childrenId", value = "宝宝id，若没有则使用默认宝宝id")
    })
    public StatusResponse getDailyReport(HttpServletRequest req, HttpServletResponse resp,
                                         @RequestHeader(TICKET) String ticket,
                                         String day, String childrenId){
        if(StringUtils.isEmpty(childrenId)) {
            WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

            if (loginUser.getDefaultBabyId() == null) {
                return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "当前家长没有就读的宝宝");
            }

            childrenId = loginUser.getDefaultBabyId();
        }

        List<CheckRecord> items = checkRecordRepository.findDayRecordByChildrenId(childrenId, day);

        return StatusResponse.success(items);
    }

}
